/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var reverseList = function (head) {
    if (!head || !head.next) return head;
    let next = head.next.next;
    let node = head.next;
    let pre = head;
    while (node) {
        node.next = pre;
        if (pre === head) pre.next = null;
        if (next) {
            pre = node;
            node = next;
            next = next.next;
        }
    }
    return node;
};